package com.loft.controller.activity;

import com.loft.R;
import com.loft.comm.MyApp;
import com.loft.comm.util.LogCenter;
import com.loft.controller.BitmapCenterController;
import com.loft.controller.activity.baseview.BaseActivity;
import com.loft.controller.activity.renren.RRBlogActivity;
import com.loft.controller.activity.renren.RenRenOauthWebActivity;
import com.loft.threadtask.DataAsynctask;
import com.loft.vo.renren.ProfileVo;
import com.loft.vo.renren.UserVo;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author heb
 * @class_name HomepageActivity.java
 * @create_date 2012-4-3
 * @version 1.0.0
 */
public class HomepageActivity extends BaseActivity {
	private final String TAG = this.getClass().getName();
	private ViewPager _viewPager;
	private List<View> _viewList;
	private ImageView _arrow_left, _arrow_right;
	private TextView _title, _topStatus;
	private ImageView _userImage;
	private TextView[][] _textItem_top = new TextView[2][3];
	private TextView[] _textItem_bottom = new TextView[5];
	private int[] _text_item_top_ids = { R.id.viewpage_item_text_line1_column1,
			R.id.viewpage_item_text_line1_column2,
			R.id.viewpage_item_text_line1_column3,
			R.id.viewpage_item_text_line2_column1,
			R.id.viewpage_item_text_line2_column2,
			R.id.viewpage_item_text_line2_column3 };

	private int[] _text_item_bottom_ids = {
			R.id.viewpage_item_linearlayout_4_1_textview,
			R.id.viewpage_item_linearlayout_4_2_textview,
			R.id.viewpage_item_linearlayout_4_3_textview,
			R.id.viewpage_item_linearlayout_4_4_textview,
			R.id.viewpage_item_linearlayout_4_5_textview, };

	private boolean _isLogined = false;
	private int _count = 0;

	private void init_top_viewGroup(View view) {
		String[] renren_item_top = getResources().getStringArray(
				R.array.renren_item_top);
		for (int i = 0; i < _textItem_top.length; i++) {
			for (int j = 0; j < _textItem_top[i].length; j++) {
				int num = i * _textItem_top[i].length + j;
				_textItem_top[i][j] = (TextView) view
						.findViewById(_text_item_top_ids[num]);
				_textItem_top[i][j].setText(renren_item_top[num]);
			}
		}
		for (int i = 0; i < _textItem_bottom.length; i++) {
			_textItem_bottom[i] = (TextView) view
					.findViewById(_text_item_bottom_ids[i]);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.homepage);
		_viewPager = (ViewPager) findViewById(R.id.homepage_pageview);
		_arrow_left = (ImageView) findViewById(R.id.homepage_title_arrow_left);
		_arrow_right = (ImageView) findViewById(R.id.homepage_title_arrow_right);
		_title = (TextView) findViewById(R.id.homepage_title);
		_userImage = (ImageView) findViewById(R.id.homepage_user_picture);
		_viewList = new ArrayList<View>();
		View view1 = getLayoutInflater().inflate(R.layout.viewpage_item, null);
		View view2 = getLayoutInflater().inflate(R.layout.viewpage_item, null);
		init_top_viewGroup(view1);
		_viewList.add(view1);
		_viewList.add(view2);
		_topStatus = (TextView) findViewById(R.id.homepage_top_status);
	}

	@Override
	protected void setAdapers() {
		_viewPager.setAdapter(new PagerAdapter() {
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return _viewList.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == (arg1);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				((ViewPager) container).removeView(_viewList.get(position));
				// Log.d("RESULT", "destroyItem"+position);
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(_viewList.get(position), 0);
				return _viewList.get(position);
			}
		});
	}

	@Override
	protected void setListeners() {
		_userImage.setOnClickListener(new ImageView.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!_isLogined) {
					Intent intent = new Intent();
					intent.setClass(HomepageActivity.this,
							RenRenOauthWebActivity.class);
					startActivityForResult(intent,
							MyApp.REQUEST_CODE_HOMEPAGE_TO_RENRENOUATHWEBVIEW);
				}
			}
		});
		_viewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

					@Override
					public void onPageSelected(int arg0) {
						switch (arg0) {
						case 0:
							_title.setText(R.string.title_renren);
							_arrow_left.setVisibility(View.GONE);
							_arrow_right.setVisibility(View.VISIBLE);
							break;
						case 1:
							_title.setText(R.string.title_rss);
							_arrow_left.setVisibility(View.VISIBLE);
							_arrow_right.setVisibility(View.GONE);
							break;
						default:
							break;
						}

					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub

					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case MyApp.REQUEST_CODE_HOMEPAGE_TO_RENRENOUATHWEBVIEW:
			LogCenter.getInstance().debug(TAG,
					"-------------->>>requestcode>>>" + requestCode);
			MyApp.RENREN_ACCESSTOKEN = data.getStringExtra("Callback");
			showDialog(DIALOG_LOADING);
			DataAsynctask dataAsynctask = new DataAsynctask(this,
					R.id.thread_flag_renren_userinfo_get, _handelr,
					MyApp.ACTIVITY_NAME_JRRGETUSERINFO, null);
			dataAsynctask.execute();
			break;
		default:
			break;
		}
	}

	public Handler _handelr = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			_count++;
			switch (msg.what) {
			case R.id.thread_flag_renren_userinfo_get:
				if (msg.obj != null) {
					UserVo userVo = ((ArrayList<UserVo>) msg.obj).get(0);
					BitmapCenterController bitmapCenterController = new BitmapCenterController(
							MyApp.ACTIVITY_NAME_IMAGELOADER,
							MyApp.SAVEPATH_RENREN);
					MyApp.RENREN_UID = userVo.getUid();
					DataAsynctask dataAsynctask2 = new DataAsynctask(
							HomepageActivity.this,
							R.id.thread_flag_renren_profile_get, _handelr,
							MyApp.ACTIVITY_NAME_JRRGETPROFILEINFO, String
									.valueOf(MyApp.RENREN_UID));
					dataAsynctask2.execute();
					bitmapCenterController.bitmapDownload(userVo.getMainurl(),
							_userImage, userVo.getName());
				} else {
					showToast(R.string.HTTPdata_read_error);
				}
				break;
			case R.id.thread_flag_renren_profile_get:
				if (msg.obj != null) {
					ProfileVo profileVo = ((ArrayList<ProfileVo>) msg.obj)
							.get(0);
					_topStatus.setText(profileVo.getStatus().getContent());
					_textItem_bottom[0].setText("留言:"
							+ profileVo.getGuestbook_count());
					_textItem_bottom[1].setText("日志:"
							+ profileVo.getBlogs_count());
					_textItem_bottom[2].setText("状态:"
							+ profileVo.getStatus_count());
					_textItem_bottom[3].setText("相册:"
							+ profileVo.getAlbums_count());
					_textItem_bottom[1]
							.setOnClickListener(HomepageActivity.this);
				} else {
					showToast(R.string.HTTPdata_read_error);
				}
				break;
			default:
				break;
			}
			if (_count == 2) {
				cancleDialog();
				_count = 0;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.viewpage_item_linearlayout_4_2_textview:
			Intent intent = new Intent();
			intent.setClass(HomepageActivity.this, RRBlogActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
			break;
		default:
			break;
		}
		super.onClick(v);
	}

}
